package com.iot.inter.controller.newinterface.trafficusage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.trafficusage.singledevice.request.RequestSingleDeviceNew;
import com.iot.inter.entity.newrequest.trafficusage.singledevice.response.ResponseSingleDeviceNew;
import com.iot.inter.interfaceprocessing.newinterface.TurnTrafficUsageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 去北京做：3.18查询单一设备流量使用情况:2.0接口
 * <p>
 * 接入方可以通过调用该接口，查询某个设备信息，包括设备流量使用情况；
 *
 * @author lm
 * @date 2019/8/5
 */
@RestController
@Api(description = "3.18查询单一设备流量使用情况")
public class TrafficUsageQuerySingleDeviceController {

    private static Logger logger = Logger.getLogger(TrafficUsageQuerySingleDeviceController.class);

    @CrossOrigin
    @RequestMapping(value = "singleDeviceTrafficUsage", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.18查询单一设备流量使用情况", response = ResponseSingleDeviceNew.class)
    public String singleDeviceTrafficUsage(@ApiParam @RequestBody RequestSingleDeviceNew request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);
        ResponseSingleDeviceNew response = new ResponseSingleDeviceNew();

        if (requestStr != null) {

            logger.info("【查询单一设备流量使用情况】请求：" + requestStr);

            RequestSingleDeviceNew requestPackage = JSONArray.parseObject(requestStr, RequestSingleDeviceNew.class);

            String deviceId = requestPackage.getDeviceId();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            TurnTrafficUsageService trafficUsageService = new TurnTrafficUsageService();
            OrderPaymentManager result = trafficUsageService.singleDevice(deviceId, secret, partnerCode);

            if (result != null) {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(result.getRetCode())) {

                    response.setOrderId(result.getOrderId());
                    response.setDeviceId(result.getDeviceId());
                    response.setOrderUsedFlow(result.getOrder_used_flow());
                    response.setHighFlowBalance(result.getHigh_flow_balance());

                    response.setRetCode(result.getRetCode());
                    response.setResultInfo(result.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(response);
                    logger.info("【查询单一设备流量使用情况】成功响应：" + jsonString);
                    return jsonString;


                } else {

                    response.setRetCode(result.getRetCode());
                    response.setResultInfo(result.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(response);
                    logger.info("【查询单一设备流量使用情况】响应：" + jsonString);
                    return jsonString;

                }

            } else {

                response.setRetCode("9998");
                response.setResultInfo("响应为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(response);
                logger.info("【查询单一设备流量使用情况】响应：" + jsonString);
                return jsonString;

            }


        } else {
            response.setRetCode("9998");
            response.setResultInfo("请求为空");
            jsonString = JSON.toJSONString(response);
            logger.info("【查询单一设备流量使用情况】响应：" + jsonString);
            return jsonString;

        }

    }


}
